package com.wuye.web;

import org.springframework.data.domain.Page;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2015/3/5.
 */
public class Pagination {

    public static String getURI(HttpServletRequest request) {
        String uri = request.getRequestURI();
        String q = request.getQueryString();
        if (q == null || q.isEmpty()) return uri + "?page=$";
        int idx = q.indexOf("page=");
        return uri + "?" + (idx >= 0 ? q.replaceFirst("page=(\\d+){0,}", "page=\\$") : q + "&page=$");
    }

    public static String getPagingString(long count, HttpServletRequest request) {
        String x = getURI(request);
        return getPagingString((int) count, ParamUtil.getInt(request, "pageSize"), ParamUtil.getInt(request, "page"), x);
    }

    public static String getPagingString(long count, int pageSize, int cpage, HttpServletRequest request) {
        String x = getURI(request);
        return getPagingString((int) count, pageSize, cpage, x);
    }

    public static String getPagingString(Page<?> page, HttpServletRequest request) {
        return getPagingString((int) page.getTotalElements(), page.getSize(), ParamUtil.getInt(request, "page"), getURI(request), 0);
    }

    public static String getPagingString(int count, int pageSize, int cpage, HttpServletRequest request) {
        String x = getURI(request);
        return getPagingString(count, pageSize, cpage, x);
    }


    public static String getPagingString(int count, int pageSize, int cpage, String url) {
        return getPagingString(count, pageSize, cpage, url, 1);
    }

    public static String getPagingString(int count, int pageSize, int cpage, String url, int pateStart) {
        int countPage = (int) Math.ceil(((double) count / pageSize));
        int page = Math.min(cpage, countPage);
        if (countPage > 1) {
            StringBuffer sb = new StringBuffer();
            sb.append("<div class=\"dataTables_paginate paging_simple_numbers\">");
            if (count > pageSize) {
                sb.append("<span>总记录:").append(count).append("</span>");
            }
            sb.append("<ul class=\"pagination pull-right\">");
            if (page > 1) {
                sb.append("<li><a href=\"" + url.replaceFirst("[$]", String.valueOf(page - 1)) + "\" class=\"paginate_button previous\"><span>上一页</span></a></li>");
            }
            if (countPage <= 11) {
                for (int i = pateStart; i <= countPage + pateStart - 1; i++) {
                    sb.append(getlink(url, page, i, pateStart));
                }
            } else {
                sb.append(getlink(url, page, 1, pateStart));
                sb.append(getlink(url, page, 2, pateStart));
                if (page > 5) {
                    sb.append("<span class=\"paginate_button\">...</span>");
                }
                int start = page - 3;
                start = Math.max(3, start);
                int end = start + 7;
                end = Math.min(end, countPage - 1);
                if (end == countPage - 1) {
                    start = end - 6;
                }
                for (int i = start; i < end; i++) {
                    sb.append(getlink(url, page, i, pateStart));
                }
                if (page + 4 < countPage) {
                    sb.append("<span class=\"paginate_button\">...</span>");
                }
                sb.append(getlink(url, page, countPage - 1, pateStart));
                sb.append(getlink(url, page, countPage, pateStart));
            }
            if (cpage < countPage) {
                sb.append("<li><a href=\"" + url.replaceFirst("[$]", String.valueOf(page + 1)) + "\" class=\"paginate_button next\"><span>下一页</span></a></li>");
            }
            sb.append("</ul></div>");
            return sb.toString();
        } else if (countPage == 1){
            StringBuffer sb = new StringBuffer();
            sb.append("<div class=\"dataTables_paginate paging_simple_numbers\">");
            sb.append("<span>总记录:").append(count).append("</span>");
            sb.append("</div>");
            return sb.toString();
        }
        return "";
    }

    private static String getlink(String url, int page, int i, int pateStart) {
        if (page == i) {
            return "<li class=\"paginate_button active\"><a style=\"color:#f7953d\">" + (i + (pateStart == 0 ? 1 : 0)) + "</a></li>";
        } else {
            return "<li><a href=\"" + url.replaceFirst("[$]", String.valueOf(i)) + "\">" + (i + (pateStart == 0 ? 1 : 0)) + "</a><li>";
        }
    }

    public static String getPagingStringWithFun(long count, int pageSize, int cpage, String fun) {
        return getPagingStringWithFun((int) count, pageSize, cpage, fun, 1);
    }
    public static String getPagingStringWithFun(long count, int pageSize, int cpage, String fun, int pateStart) {
        int countPage = (int) Math.ceil(((double) count / pageSize));
        int page = Math.min(cpage, countPage);
        if (countPage > 1) {
            StringBuffer sb = new StringBuffer();
            sb.append("<div class=\"dataTables_paginate paging_simple_numbers\">");
            sb.append("<ul class=\"pagination pull-right\">");
            if (count > pageSize) {
                sb.append("<li style=\"font-size: 16px;\">总记录:").append(count).append("</li>");
            }
            if (page > 1) {
                sb.append("<li><a href=\"" + "javascript:void(0);" + "\" onclick=\"" + fun + "(" + (page - 1) +",'" + fun + "')" +"\" class=\"paginate_button previous\"><span>上一页</span></a></li>");
            }
            if (countPage <= 11) {
                for (int i = pateStart; i <= countPage + pateStart - 1; i++) {
                    sb.append(getlinkWithFun(fun, page, i, pateStart));
                }
            } else {
                sb.append(getlinkWithFun(fun, page, 1, pateStart));
                sb.append(getlinkWithFun(fun, page, 2, pateStart));
                if (page > 5) {
                    sb.append("<span class=\"paginate_button\">...</span>");
                }
                int start = page - 3;
                start = Math.max(3, start);
                int end = start + 7;
                end = Math.min(end, countPage - 1);
                if (end == countPage - 1) {
                    start = end - 6;
                }
                for (int i = start; i < end; i++) {
                    sb.append(getlinkWithFun(fun, page, i, pateStart));
                }
                if (page + 4 < countPage) {
                    sb.append("<span class=\"paginate_button\">...</span>");
                }
                sb.append(getlinkWithFun(fun, page, countPage - 1, pateStart));
                sb.append(getlinkWithFun(fun, page, countPage, pateStart));
            }
            if (cpage < countPage) {
                sb.append("<li><a href=\"" + "javascript:void(0);" + "\" onclick=\"" + fun + "(" + (page + 1) +",'" + fun + "')" +"\" class=\"paginate_button next\"><span>下一页</span></a></li>");
            }
            sb.append("</ul></div>");
            return sb.toString();
        }
        return "";
    }

    private static String getlinkWithFun(String fun, int page, int i, int pateStart) {
        if (page == i) {
            return "<li class=\"paginate_button active\"><a style=\"color:#f7953d\">" + (i + (pateStart == 0 ? 1 : 0)) + "</a></li>";
        } else {
            return "<li><a href=\"" + "javascript:void(0);" + "\" onclick=\"" + fun + "(" + i +",'" + fun + "')" +"\">" + (i + (pateStart == 0 ? 1 : 0)) + "</a><li>";
        }
    }
}
